/*
 * 选择A/R账号
 * 2017-03-14
 */
;(function ($) {
	$.fn.jqGridFnModalSelAr=function(options){
		//获取jquery对象
		var grid_selector = this;
		
		//初始化
		var init = {
			oper:"",
			pager_selector:"",
			height:"",
			width:"1000",
			url:"",
			where:{},
			wherestring:{},	 
			offset:0,
			onShow:function(){},
			rowNum:10,
			fileds:{"fileds":["status_c","status","gno","aname","text",
			                  "dcode","shareno","sname","vname","phone",
			                  "password","ardate","time_s","postlimit","remark","balance_c","dcode_c","signaturebase64"]},
			table:{"table":{"table":"viewJqGrid_ho_arcust_marcust"}},
			callBack:function(){},
			onSelectBack : function(){},
		};
		
		//初始化参数
		var initDate = $.extend(init, options);
		//执行操作
		switch(initDate.oper){
			case 'jqGridCre':jqGridCre(initDate,grid_selector);break;
			case 'jqGridFre':jqGridFre(initDate,grid_selector);break;
		};
	};
			
	/*
	 * 私有函数
	 * 初始化jqgrid
	 * 2017-03-14
	 */
	function jqGridCre(initDate,grid_selector){
		//表格位置
		$(window).on("resize.jqGrid", function() {   
			grid_selector.jqGrid("setGridWidth", initDate.width);
		})  		
		var parent_column = grid_selector.closest("[class*='col-']");
		$(document).on("settings.ace.jqGrid", function(ev, event_name, collapsed) {
			if (event_name === "sidebar_collapsed" || event_name === "main_container_fixed") {
				setTimeout(function() {
					grid_selector.jqGrid("setGridWidth", parent_column.width());
				},0);
			}
		});
		
		//构造表格数据
		var initc=jQuery.extend({}, initDate.where,initDate.fileds,initDate.table,initDate.wherestring);
		
		//构造表格
		jQuery(grid_selector).jqGrid({
			subGrid : false,
			datatype : "local",
			height : initDate.height,
			colNames : ["G","状态","状态","账号/卡号","A/R账号名称","电话","可挂账金额","余额","组别","共享","GNO","签单人","担保人","密码","总次数","日次数","备注","开始时间","","","积分卡No","积分卡名称"],
			colModel : [{
					name : "G",//"状态", 
					index : "G",
					align : "center",
					width:30,
					formatter : function(cellvalue,options,rowObject){
						var str = '';
						if(rowObject.gno<0){
							str = '<span style="font-size:15px;font-weight:bold;color:#6fb3e0;">G</span>';
						}
						return str;
					}
				},{
					name : "status",//状态
					index : "status",
					width: 60,
					formatter:function(cellvalue, options, rowObject){ 
						var str ="";
						if(cellvalue==0){
							str = '<span class="label label-danger">'+$.i18n.propJsp("禁用")+'</span>'
						}else if(cellvalue==-1){
							str = '<span class="label label-inverse"><s >&nbsp;'+$.i18n.propJsp("注销")+'&nbsp;</s></span>'
						}else if(cellvalue==1){
							
						}
						return str;
					},
				},{
					name : "status_c",//"状态", 
					index : "status_c",
					width:50,
					hidden:true,
				},{
					name : "aname",//"账号/卡号",
					index : "aname",
					width:120,
				},{
					name : "text",//"A/R账号名称",
					index : "text",
					width:120,
					
				},{
					name : "phone",//"电话",
					index : "phone",
					width:100,
				},{
					name : "postlimit",//"可挂账金额",
					index : "postlimit",
					width:100,
					align : "right"
				},{
					name : "balance_c",//"余额",
					index : "balance_c",
					width:100,
					align : "right",
					hidden : true,
				},{
					name : "dcode_c",//"组别",
					index : "dcode_c",
					width:100,
				},{
					name : "shareno",//"共享",
					index : "shareno",
					width:50,
				},{
					name : "gno",//"GNO",
					index : "gno",
					width:80,
				},{
					name : "sname",//"签单人",
					index : "sname",
					width:80,
					hidden : true,
				},{
					name : "vname",//"担保人",
					index : "vname",
					width:80,
					hidden : true,
				},{
					name : "password",//"密码",
					index : "password",
					hidden:true,
				},{
					name : "time_s",//"总次数",
					index : "time_s",
					width:50,
					hidden : true,
				},{
					name : "day_s",//"日次数",
					index : "day_s",
					width:60,
					hidden : true,
				},{
					name : "remark",//"备注",
					index : "remark",
					width:100,
					hidden : true,
				},{
					name : "ardate",//"开始时间",
					index : "ardate",
					width:100,
					hidden : true,
				},{
					name : "dcode",//"",
					index : "dcode",
					hidden:true,
				},{
					name : "signaturebase64",//""
					index : "signaturebase64",
					hidden:true,
				},{
					name : "mar_mno",//"",
					index : "mar_mno",
					hidden:true,
				},{
					name : "mar_mname",//""
					index : "mar_mname",
					hidden:true,
				}],    
				
			sortname : "aname",
			sortorder : "asc",
			viewrecords : true,
			multiselect : false,
			rowNum : initDate.rowNum,
			pager : initDate.pager_selector,
			emptyrecords: $.i18n.propJsp("没有搜索到任何数据"),
			recordtext: $.i18n.propJsp("共计")+":{2}"+$.i18n.propJsp("条"),
			altRows : true,  
			shrinkToFit:false,
//			autowidth:true,
			scroll: true,
			page:1,
	        ondblClickRow: function(rowid) {
	        	initDate.callBack();
	        },
	        onSelectRow:function(rowid){
		    	rowdata = grid_selector.jqGrid("getRowData",rowid);
		    	initDate.onSelectBack(rowdata);
	        },
			loadComplete : function(xhr) {
				var table = this;
				setTimeout(function(){
					updatePagerIcons(table);
					enableTooltips(table);
				}, 0);
			}, 
		});
		$(window).triggerHandler("resize.jqGrid");  
		
		jqGridFre(initDate,grid_selector);
	}
	
	/*
	 * 私有函数
	 * 刷新jqgrid
	 * 2017-03-14
	 */
	function jqGridFre(initDate,grid_selector){
		//刷新
		jQuery(grid_selector).jqGrid('setGridParam',{
		   url:$.HSAPI_AJAXURL+"/qms/mobile/procedure2",      
		   postData :initDate.postData,	
		   page:1,
		   datatype : 'json',
		   rowNum : 20,//initDate.rowNum
//	       serializeGridData:function(postData){
//	    	   var item={};
//	    	   item["rows"]=postData.rows;
//	    	   item["page"]=postData.page;
//	    	   item["sidx"]=postData.sidx;
//	    	   item["sord"]=postData.sord;
//	    	   postData.jnJson2="["+JSON.stringify(item)+"]";
//	    	   return postData
//	       },
       }).trigger('reloadGrid');
		
	};
	
	/*
	 * 私有函数
	 * 翻页栏
	 * 2017-03-14
	 */
	function updatePagerIcons(table) {
		var replacement = {
				"ui-icon-seek-first" : "ace-icon fa fa-angle-double-left bigger-140",
				"ui-icon-seek-prev" : "ace-icon fa fa-angle-left bigger-140",
				"ui-icon-seek-next" : "ace-icon fa fa-angle-right bigger-140",
				"ui-icon-seek-end" : "ace-icon fa fa-angle-double-right bigger-140"
		};
		$(".ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon").each(function() {
			var icon = $(this);
			var $class = $.trim(icon.attr("class").replace("ui-icon", ""));

			if ($class in replacement)
				icon.attr("class", "ui-icon " + replacement[$class]);
		})
	};
	
	/*
	 * 私有函数
	 * 提示框
	 * 2017-03-14
	 */
	function enableTooltips(table) {
		$('.navtable .ui-pg-button').tooltip({
			container : 'body'
		});
		$(table).find('.ui-pg-div').tooltip({
			container : 'body'
		});
	};
	
})(jQuery);